﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SampleCodeReportLibrary.Extensions;

namespace SampleCodeReportLibrary
{
    public static class SampleCode
    {
        public static System.Data.DataTable GetDefaultPalette()
        {
            var p = GetDefaultPaletteX();
            var dt = new System.Data.DataTable();
            dt.Columns.Add("Num", typeof(int));
            dt.Columns.Add("HexValue", typeof(string));

            for (int i = 0; i < p.Items.Count; i++)
            {
                var c = System.Drawing.Color.FromArgb(p.Items[i].rgb);
                dt.Rows.Add((i + 1).ToString(), System.Drawing.ColorTranslator.ToHtml(c));
            }

            return dt;
        }

        public static System.Data.DataTable GetDefaultPaletteVariations()
        {
            var p = GetDefaultPaletteX();
            var dt = new System.Data.DataTable();
            dt.Columns.Add("Num", typeof(int));
            dt.Columns.Add("Color", typeof(string));
            dt.Columns.Add("Dark1", typeof(string));
            dt.Columns.Add("Dark2", typeof(string));
            dt.Columns.Add("Dark3", typeof(string));
            dt.Columns.Add("Dark4", typeof(string));
            dt.Columns.Add("Dark5", typeof(string));
            dt.Columns.Add("Light1", typeof(string));
            dt.Columns.Add("Light2", typeof(string));
            dt.Columns.Add("Light3", typeof(string));
            dt.Columns.Add("Light4", typeof(string));
            dt.Columns.Add("Light5", typeof(string));

            for (int i = 0; i < p.Items.Count; i++)
            {
            }

            return dt;
        }

        public static Palette GetDefaultPaletteX()
        {
            var colors = new int [] { 
               0x4572a7, 
               0xc0504d, 
               0x9bbb28, 
               0x8064a2, 
               0x4bacc6, 
               0xf79646, 
               0xfdd600, 
               0x2185ff, 
               0xce3b37, 
               0x6bbd46, 
               0x834ec5, 
               0x21c1ed, 
               0xfa6d10, 
               0xfff114, 
               0x9db3d9, 
               0xdb9d9d, 
               0xc2d7a1, 
               0xb4a7c8 };

            var p = new Palette();
            foreach (int color in colors)
            {
                p.Add(color);
            }
            return p;
        }


        public static string GetSpecialReportTitle()
        {
            return "Special Title from SampleCodeReportLibrary";
        }

        public static System.Data.DataTable TextToDataTable(string text)
        {
            // get all the cleanup up nonempty lines
            var lines = text
                .quicksplit('\n')
                .Where(s => s.Length > 0)
                .ToList();

            if (lines.Count < 1)
            {
                throw new System.ArgumentException("text doesn' contain enough lines of data");
            }

            string first_line = lines[0];
            var first_line_tokens = first_line.quicksplit('|');
            int num_cols = first_line_tokens.Length;

            var typedic = new Dictionary<string, System.Type>
        {
            {"bool",typeof(bool)},
            {"string",typeof(string)},
            {"int",typeof(int)},
            {"double",typeof(double)},
            {"datetime",typeof(System.DateTime)}
        };

            var dt = new System.Data.DataTable();
            foreach (string token in first_line_tokens)
            {
                var subtokens = token.quicksplit(':');
                string colname = subtokens[0];
                string typename = subtokens[1];
                var coltype = typedic[typename];
                dt.Columns.Add(colname, coltype);
            }

            var row_lines = lines.Skip(1);
            dt.BeginLoadData();
            var data = new object[num_cols];
            foreach (string line in row_lines)
            {
                var subtokens = line.quicksplit('|');
                for (int i = 0; i < num_cols; i++)
                {
                    data[i] = null;
                }

                for (int col_index = 0; col_index < subtokens.Length && col_index < num_cols; col_index++)
                {
                    var subtoken = subtokens[col_index];
                    var col = dt.Columns[col_index];
                    if (col.DataType == typeof(int))
                    {
                        data[col_index] = int.Parse(subtoken);
                    }
                    else if (col.DataType == typeof(bool))
                    {
                        data[col_index] = bool.Parse(subtoken);
                    }
                    else if (col.DataType == typeof(double))
                    {
                        data[col_index] = double.Parse(subtoken);
                    }
                    else if (col.DataType == typeof(System.DateTime))
                    {
                        data[col_index] = System.DateTime.Parse(subtoken);
                    }
                    else if (col.DataType == typeof(string))
                    {
                        data[col_index] = subtoken;
                    }
                    else
                    {
                    }
                }
                dt.Rows.Add(data);
            }
            dt.EndLoadData();
            return dt;
        }
 

        public static System.Data.DataTable GetDotNETMethodNames()
        {
            var dt = new System.Data.DataTable();
            dt.Columns.Add("TypeName", typeof(string));
            dt.Columns.Add("TypeFullName", typeof(string));
            dt.Columns.Add("MethodName", typeof(string));
            var asm = typeof(string).Assembly;
            var all_exported_types = asm.GetExportedTypes();
            var types = all_exported_types.Take(20);

            dt.BeginLoadData();
            foreach (var t in types)
            {
                foreach (var m in t.GetMethods().Take(10))
                {
                    if (m.IsPublic)
                    {
                        dt.Rows.Add(t.Name, t.FullName, m.Name);
                    }
                }
            }
            dt.EndLoadData();

            return dt;
        }


        public static System.Data.DataTable GetAXEnum(string enumname)
        {
            if (enumname == null)
            {
                throw new System.ArgumentNullException("enumname");
            }
            
            var dt = new System.Data.DataTable();
            dt.Columns.Add("Symbol", typeof(string));
            dt.Columns.Add("IntValue",typeof(int));
            dt.Columns.Add("Label",typeof(string));

            var ax = Microsoft.Dynamics.Framework.Reports.SessionManager.GetSession();
            int enum_id = (int)ax.CallStaticClassMethod("Global", "enumName2Id", enumname);
            var enum_obj = ax.CreateAxaptaObject("DictEnum", Convert.ToString(enum_id));
            int val_count = (int)enum_obj.Call("values");

            dt.BeginLoadData();

            for (int i = 0; i < val_count; i++)
            {
                string symbol = (string)enum_obj.Call("value2Symbol", i);

                if (symbol.Length > 0)
                {
                    int int_value = (int)enum_obj.Call("symbol2Value", symbol);
                    string label = (string)enum_obj.Call("value2Label", i);
                    dt.Rows.Add(symbol, int_value, label);
                }
            }
            dt.EndLoadData();
            
            return dt;
        }

        public static System.Data.DataTable GetEmptyDataTable()
        {
            var dt = new System.Data.DataTable();
            dt.Columns.Add("FirstName", typeof(string));
            dt.Columns.Add("LastName", typeof(string));
            dt.Columns.Add("Age", typeof(int));
            return dt;
        }

        public static System.Drawing.Color GetNiceLabelColor(System.Drawing.Color bkcolor)
        {
            float brightness = bkcolor.GetBrightness();
            if (brightness > 0.5)
            {
                return System.Drawing.Color.Black;
            }
            else
            {
                return System.Drawing.Color.White;
            }
        }

        public static System.Data.DataTable GetPalettes(string url)
        {
            var dt = new System.Data.DataTable();
            dt.Columns.Add("Title", typeof(string));
            dt.Columns.Add("UserName", typeof(string));
            dt.Columns.Add("ID", typeof(string));
            dt.Columns.Add("Color1", typeof(string));
            dt.Columns.Add("Color2", typeof(string));
            dt.Columns.Add("Color3", typeof(string));
            dt.Columns.Add("Color4", typeof(string));
            dt.Columns.Add("Color5", typeof(string));

            dt.BeginLoadData();

            var xdoc = System.Xml.Linq.XDocument.Load(url);

            var root_el = xdoc.Root;
            var palettes = root_el.Elements("palette");

            foreach (var pal_el in palettes)
            {
                var id = pal_el.Element("id").Value;
                var username = pal_el.Element("userName").Value;
                var title = pal_el.Element("title").Value;

                var colors_el = pal_el.Element("colors");
                var hex_elements = colors_el.Elements("hex").ToList();

                var int_values = hex_elements.Select(el => Convert.ToInt32(el.Value, 16)).ToList();
                var hex_values = hex_elements.Select(el => "#" + el.Value).ToList();

                dt.Rows.Add(title, username, id,
                    hex_values[0], hex_values[1], hex_values[2], hex_values[3], hex_values[4]);
            }

            dt.EndLoadData();
            return dt;
        }


        public static System.Data.DataTable GetFillerData(int num_rows, int num_cols)
        {
            var dt = new System.Data.DataTable();
            for (int i = 0; i < num_cols; i++)
            {
                dt.Columns.Add(string.Format("Col{0:00}", i), typeof(string));
            }

            var rand = new System.Random();
            dt.BeginLoadData();

            var obj_array = new object[num_cols];
            for (int r = 0; r < num_rows; r++)
            {
                for (int c = 0; c < num_cols; c++)
                {
                    obj_array[c] = string.Format("{0:0.0}", rand.NextDouble() * 10.0);
                }
                dt.Rows.Add(obj_array);
            }
            dt.EndLoadData();

            return dt;
        }

        public static System.Data.DataTable GetCashFlow( System.DateTime startdate, int num)
        {

            var data = new[] { 
                new {  inflow=415.0, outflow=325.0  , date=new System.DateTime(2011,02,06) },
                new {  inflow=362.0, outflow=284.0 , date=new System.DateTime(2011,01,30) },
                new {  inflow=114.0, outflow=80.0 , date=new System.DateTime(2011,01,23) },
                new {  inflow=156.0, outflow=102.0 , date=new System.DateTime(2011,01,16) },
                new {  inflow=155.0, outflow=93.0 , date=new System.DateTime(2011,01,09) },
                new {  inflow=220.0, outflow=157.0 , date=new System.DateTime(2011,01,02) },
                new {  inflow=108.0, outflow=77.0 , date=new System.DateTime(2010,12,26) },
                new {  inflow=89.0, outflow=53.0 , date=new System.DateTime(2010,12,19) },
                new {  inflow=77.0, outflow=56.0 , date=new System.DateTime(2010,12,12) },
                new {  inflow=75.0, outflow=63.0 , date=new System.DateTime(2010,12,25) },
                new {   inflow=91.0, outflow=65.0 , date=new System.DateTime(2010,11,28) },
                new {   inflow=91.0, outflow=75.0 , date=new System.DateTime(2010,11,21) },
                new {   inflow=129.0, outflow=75.0 , date=new System.DateTime(2010,11,14) },
                new {   inflow=85.0, outflow=73.0 , date=new System.DateTime(2010,11,07) },
            };

            var dt = new System.Data.DataTable();
            dt.Columns.Add("Year", typeof(int));
            dt.Columns.Add("Month", typeof(int));
            dt.Columns.Add("Date", typeof(System.DateTime));
            dt.Columns.Add("Index", typeof(int));
            dt.Columns.Add("InFlow", typeof(double));
            dt.Columns.Add("OutFlow", typeof(double));
            dt.Columns.Add("NetFlow", typeof(double));
            dt.Columns.Add("PaddingBelow", typeof(double));


            dt.BeginLoadData();

            double max_outflow = data.Select(i => i.outflow).Max();
            int n=0;
            foreach (var item in data )
            {
                var curdate = startdate.AddDays(-n);
                double padding = max_outflow - item.outflow;
                dt.Rows.Add(curdate.Year, curdate.Month, curdate, n, item.inflow, -item.outflow, item.inflow + item.outflow, -padding);
                n++;

                if (n >= num)
                {
                    break;
                } ;
            }

            dt.EndLoadData();
            return dt;
        }

        public static System.Data.DataTable GetWeeklyBlogStats(System.DateTime startdate, int num)
        {

            var data = new[] { 
                new {  pageviews=415.0, uniquevisitors=325.0  },
                new {  pageviews=362.0, uniquevisitors=284.0    },
                new {  pageviews=114.0, uniquevisitors=80.0     },
                new {  pageviews=156.0, uniquevisitors=102.0    },
                new {  pageviews=155.0, uniquevisitors=93.0     },
                new {  pageviews=220.0, uniquevisitors=157.0    },
                new {  pageviews=108.0, uniquevisitors=77.0     },
                new {  pageviews=89.0, uniquevisitors=53.0      },
                new {  pageviews=77.0, uniquevisitors=56.0      },
                new {  pageviews=75.0, uniquevisitors=63.0      },
                new {   pageviews=91.0, uniquevisitors=65.0     },
                new {   pageviews=91.0, uniquevisitors=75.0     },
                new {   pageviews=129.0, uniquevisitors=75.0    },
                new {   pageviews=85.0, uniquevisitors=73.0    },
            };

            var dt = new System.Data.DataTable();
            dt.Columns.Add("Year", typeof(int));
            dt.Columns.Add("Month", typeof(int));
            dt.Columns.Add("Date", typeof(System.DateTime));
            dt.Columns.Add("Index", typeof(int));
            dt.Columns.Add("PageViews", typeof(double));
            dt.Columns.Add("UniqueVisitors", typeof(double));


            dt.BeginLoadData();

            double max_outflow = data.Select(i => i.uniquevisitors).Max();
            int n = 0;
            foreach (var item in data)
            {
                var curdate = startdate.AddDays(-(n*7));
                dt.Rows.Add(curdate.Year, curdate.Month, curdate, n, item.pageviews, item.uniquevisitors);
                n++;

                if (n >= num)
                {
                    break;
                };
            }

            dt.EndLoadData();
            return dt;
        }

        public static System.Data.DataTable GetChartData1()
        {
            var dt = new System.Data.DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Value", typeof(int));

            dt.Rows.Add("A", 10);
            dt.Rows.Add("B", 12);
            dt.Rows.Add("C", 17);
            dt.Rows.Add("D", 14);
            dt.Rows.Add("E", 21);
            dt.Rows.Add("F", 23);
            dt.Rows.Add("G", 22);

            return dt;
        }
    }
}
